#include <iostream>
#include <poll.h>
#include <unistd.h>

int main()
{
    struct pollfd rfds;
    rfds.fd = 0;
    rfds.events = POLLIN;
    rfds.revents = 0;

    //第一个参数代表数据在哪里
    //第二个参数代表存放数据的结构体个数
    //第三个参数代表等待时间，单位是毫秒(ms)
    while (true)
    {

        int n = poll(&rfds, 1, 1000);
        switch (n)
        {
        case 0:
            std::cout << "time out" << std::endl;
            break;
        case -1:
            std::cerr << "poll error" << std::endl;
            break;
        default:
            std::cout << "有事件发生" << std::endl;
            //有事件发生后revents会被操作系统设置成1
            if (rfds.revents & POLLIN)
            {
                std::cout << rfds.fd << "读事件发生" << std::endl;
                char buffer[1024];
                ssize_t s = read(0, buffer, sizeof(buffer) - 1);
                if (s > 0)
                {
                    std::cout << "有人说# " << buffer << std::endl;
                }
            }
            break;
        }
    }
    return 0;
}